import { dbUtils } from '../core'

export interface StoreShopDeskRecordRow {
    id: string
    store_id: string
    shop_id: string
    desk_id: string
    order_id: string
    user_id: string
    clerk_id: string
    is_book: number
    book_id: string
    book_time: number
    keep_time: number
    book_status: number
    use_num: number
    adult_num: number
    elderly_num: number
    children_num: number
    open_type: number
    target_record_id: string
    settle_type: number
    open_num: number
    money: number
    status: number
    start_time: number
    end_time: number
    is_vip: number
    member_discount: number
    order_discount: number
    is_lock: number
    user_lock: number
    is_used: number
    desk_no: string
    is_delete: number
    remark: string
    update_time: number
    create_time: number
}

export const storeShopDeskRecordRepository = {
    upsertBatch(rows: StoreShopDeskRecordRow[]): void {
        if (!rows || rows.length === 0) return
        dbUtils.transaction(() => {
            const sql = `INSERT INTO store_shop_desk_record (
                id, store_id, shop_id, desk_id, order_id, user_id, clerk_id, is_book, book_id, book_time, keep_time,
                book_status, use_num, adult_num, elderly_num, children_num, open_type, target_record_id, settle_type,
                open_num, money, status, start_time, end_time, is_vip, member_discount, order_discount, is_lock,
                user_lock, is_used, desk_no, is_delete, remark, update_time, create_time
            ) VALUES (
                ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
            )
            ON CONFLICT(id) DO UPDATE SET
                store_id=excluded.store_id,
                shop_id=excluded.shop_id,
                desk_id=excluded.desk_id,
                order_id=excluded.order_id,
                user_id=excluded.user_id,
                clerk_id=excluded.clerk_id,
                is_book=excluded.is_book,
                book_id=excluded.book_id,
                book_time=excluded.book_time,
                keep_time=excluded.keep_time,
                book_status=excluded.book_status,
                use_num=excluded.use_num,
                adult_num=excluded.adult_num,
                elderly_num=excluded.elderly_num,
                children_num=excluded.children_num,
                open_type=excluded.open_type,
                target_record_id=excluded.target_record_id,
                settle_type=excluded.settle_type,
                open_num=excluded.open_num,
                money=excluded.money,
                status=excluded.status,
                start_time=excluded.start_time,
                end_time=excluded.end_time,
                is_vip=excluded.is_vip,
                member_discount=excluded.member_discount,
                order_discount=excluded.order_discount,
                is_lock=excluded.is_lock,
                user_lock=excluded.user_lock,
                is_used=excluded.is_used,
                desk_no=excluded.desk_no,
                is_delete=excluded.is_delete,
                remark=excluded.remark,
                update_time=excluded.update_time,
                create_time=excluded.create_time,
                updated_at=CURRENT_TIMESTAMP`

            for (const r of rows) {
                dbUtils.execute(sql, [
                    r.id, r.store_id, r.shop_id, r.desk_id, r.order_id, r.user_id, r.clerk_id, r.is_book, r.book_id,
                    r.book_time, r.keep_time, r.book_status, r.use_num, r.adult_num, r.elderly_num, r.children_num,
                    r.open_type, r.target_record_id, r.settle_type, r.open_num, r.money, r.status, r.start_time,
                    r.end_time, r.is_vip, r.member_discount, r.order_discount, r.is_lock, r.user_lock, r.is_used,
                    r.desk_no, r.is_delete, r.remark, r.update_time, r.create_time
                ])
            }
        })
    }
}

export default storeShopDeskRecordRepository


